package cn.lrwjz.androidguide.ui;

import android.os.Bundle;
import android.view.MenuItem;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import com.google.android.material.appbar.MaterialToolbar;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;

import cn.lrwjz.androidguide.R;

/**
 * 标签示例Activity
 * 展示Material Design标签组件的使用方法
 */
public class ChipActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_chip);

        // 设置工具栏
        MaterialToolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        if (getSupportActionBar() != null) {
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setTitle("Chip 示例");
        }

        // 基础标签点击事件
        Chip basicChip = findViewById(R.id.basicChip);
        basicChip.setOnClickListener(v -> 
            Toast.makeText(this, "点击了基础标签", Toast.LENGTH_SHORT).show());

        // 可关闭标签事件
        Chip closeableChip = findViewById(R.id.closeableChip);
        closeableChip.setOnCloseIconClickListener(v -> {
            closeableChip.setVisibility(Chip.GONE);
            Toast.makeText(this, "关闭了标签", Toast.LENGTH_SHORT).show();
        });

        // 带图标标签点击事件
        Chip iconChip = findViewById(R.id.iconChip);
        iconChip.setOnClickListener(v -> 
            Toast.makeText(this, "点击了带图标标签", Toast.LENGTH_SHORT).show());

        // 选择标签组事件
        ChipGroup chipGroup = findViewById(R.id.chipGroup);
        chipGroup.setOnCheckedStateChangeListener((group, checkedIds) -> {
            if (checkedIds.size() > 0) {
                Chip chip = findViewById(checkedIds.get(0));
                Toast.makeText(this, 
                    "选择了: " + chip.getText(), 
                    Toast.LENGTH_SHORT).show();
            }
        });

        // 过滤标签组事件
        ChipGroup filterChipGroup = findViewById(R.id.filterChipGroup);
        filterChipGroup.setOnCheckedStateChangeListener((group, checkedIds) -> {
            StringBuilder selected = new StringBuilder("已选择: ");
            for (int id : checkedIds) {
                Chip chip = findViewById(id);
                selected.append(chip.getText()).append(" ");
            }
            Toast.makeText(this, selected.toString(), Toast.LENGTH_SHORT).show();
        });
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
} 